package cn.herlioner.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.herlioner.dao.Obj_TeaDAO;
import cn.herlioner.pojo.Obj_Tea;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.service.PageService;

public class Obj_TeaDAOImpl implements Obj_TeaDAO{

	@Override
	public void add(Obj_Tea ot) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		
		s.save(ot);
		
		s.getTransaction().commit();
		s.close();
		sf.close();
		
	}

	@Override
	public Obj_Tea getbyid(Long id) {
		Obj_Tea result = null;
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        result = (Obj_Tea) s.get(Obj_Tea.class, id);
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public void delete(Long id) {
		SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        Obj_Tea p = (Obj_Tea) s.get(Obj_Tea.class, id);
 
        s.delete(p);
 
        s.getTransaction().commit();
        s.close();
        sf.close();
		
	}

	@Override
	public void update(Obj_Tea ot) {
		 SessionFactory sf = new Configuration().configure()
	                .buildSessionFactory();
	        Session s = sf.openSession();
	        s.beginTransaction();
	 
	        s.update(ot);
	 
	        s.getTransaction().commit();
	        s.close();
	        sf.close();
		
	}

	@Override
	public List<Obj_Tea> list() {
		List<Obj_Tea> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        Query q = s.createQuery("from Obj_Tea ot");
 
        result = q.list();
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public Obj_Tea getbyteanum(Long teanum) {
		Obj_Tea result = null;
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        result = (Obj_Tea) s.get(Obj_Tea.class, teanum);
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public Obj_Tea getbyobjnum(Long objnum) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		List<Obj_Tea> list = s
				.createQuery("from Obj_Tea where objnum = :objnum")
				.setLong("objnum", objnum).list();
		if (list.size() == 0) {
			s.close();
			sf.close();
			return null;// 未找到这个用�?
		}
		s.close();
		sf.close();
		return list.get(0);
	}

	@Override
	public List<Obj_Tea> listbyobjnum(Long objnum) {
		List<Obj_Tea> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        List<Obj_Tea> list = s.createQuery("from Obj_Tea where objnum = :objnum")
        		.setLong("objnum", objnum).list();
 
        if (list.size() == 0) {
			s.close();
			sf.close();
			return null;
		}
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public PageService listbyteanum(int pagenum, int pageSize, Long teanum) {
		List<Obj_Tea> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        List<Obj_Tea> list = s.createQuery("from Obj_Tea where teanum = :teanum")
        		.setLong("teanum", teanum)
        		.setFirstResult(((pagenum - 1) * pageSize))
        		.setMaxResults(pageSize).list();
        		
        //查询总数
        Long count = (Long) s.createQuery(//  
                "SELECT COUNT(*) FROM Obj_Tea o where teanum = :teanum")
                .setLong("teanum", teanum)//  
                .uniqueResult(); 
        
        
        if (list.size() == 0) {
			s.close();
			sf.close();
			return null;
		}
 
        s.close();
        sf.close();
        return new PageService(pagenum, pageSize, count.intValue(), list);
	}
	
	public List<Obj_Tea> listbyteanum(Long long1) {
		List<Obj_Tea> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        List<Obj_Tea> list = s.createQuery("from Obj_Tea where teanum = :teanum")
        		.setLong("teanum", long1).list();
 
        if (list.size() == 0) {
			s.close();
			sf.close();
			return null;
		}
        result = list;
        
        s.close();
        sf.close();
        return result;
	}

}
